<template>
  <!-- // 分配角色 -->
  <div>
    <div>
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
    </div>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>

import { getRoles, assignRole } from '@/api/settings'
import { getUserDetaiById } from '@/api/user'

export default {
  props: {
    curEmployeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 当前选中的角色的id
      roleList: [] // 当前所有的角色信息
    }
  },
  created() {
    // 获取系统中已有的角色列表
    this.loadRoles()
    // 获取当前的用户具备的角色
    this.loadRolesById()
  },
  methods: {
    async hSave() {
      await assignRole(this.curEmployeeId, this.roleIds)
      // 操作成功
      this.$emit('success')
    },
    async loadRolesById() {
      // 调用接口,传入id
      const res = await getUserDetaiById(this.curEmployeeId)
      this.roleIds = res.data.roleIds
    },
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 1000 })
      console.log('--roles', res)
      this.roleList = res.data.rows
    },
    closeDialog() {

    }
  }
}
</script>
